import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.elasticsearch.spark._

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2019/10/14 10:59
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_tb_cate {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    val values: RDD[String] = sc.esJsonRDD(s"2019_tmall/tmall_2019_10",
      """
        |{
        |  "query": {
        |    "term": {
        |      "shopName.keyword": {
        |        "value": "天猫优品官方直营"
        |      }
        |    }
        |  }
        |}
      """.stripMargin).values
    val frame1: DataFrame = sqlContext.read.json(values)

    import org.apache.spark.sql.functions._
   val frame = frame1.withColumn("provinces",lit("浙江省"))
                        .withColumn("addresss",lit("浙江省杭州市滨江区长河街道网商路699号1号楼3楼310室"))
                        .withColumn("citys",lit("杭州市"))
                        .withColumn("districts",lit("滨江区"))
                        .withColumn("regional_IDs",lit("330108"))
                        .withColumn("district_origins",lit("滨江区"))
                        .withColumn("city_origins",lit("杭州市"))
                        .withColumn("administrative_regions",lit("华东地区"))
                        .withColumn("urban_agglomerationss",lit("1"))
                        .withColumn("city_grades",lit("2"))
                        .withColumn("economic_divisions",lit("2"))
                        .withColumn("registration_institutions",lit("杭州市高新区（滨江）市场监督管理局"))
                        .withColumn("aedzids",lit("-1"))
                        .withColumn("if_citys",lit("1"))
                        .withColumn("if_districts",lit("1"))
                        .withColumn("if_state_level_new_areass",lit("0"))
                        .withColumn("latitudes",lit("-1"))
                        .withColumn("longitudes",lit("-1"))
                        .withColumn("poor_countiess",lit("0"))
                        .withColumn("rural_demonstration_countiess",lit("0"))
                        .withColumn("rural_ecommerces",lit("0"))
                        .withColumn("the_belt_and_road_citys",lit("0"))
                        .withColumn("the_belt_and_road_provinces",lit("0"))
                        .withColumn("the_yangtze_river_economic_zone_citys",lit("0"))
                        .withColumn("the_yangtze_river_economic_zone_provinces",lit("0"))
                        .withColumn("towns",lit("-1"))
      .drop("province","address","city","district","regional_ID","district_origin","city_origin","administrative_region",
      "urban_agglomerations","city_grade","economic_division","registration_institution","aedzid",
      "if_city","if_district","if_state_level_new_areas","latitude","longitude","poor_counties","rural_demonstration_counties",
      "rural_ecommerce","the_belt_and_road_city","the_belt_and_road_province","the_yangtze_river_economic_zone_city",
      "the_yangtze_river_economic_zone_province","town")
      .withColumnRenamed("provinces","province")
      .withColumnRenamed("addresss","address")
      .withColumnRenamed("citys","city")
      .withColumnRenamed("districts","district")
      .withColumnRenamed("regional_IDs","regional_ID")
      .withColumnRenamed("district_origins","district_origin")
      .withColumnRenamed("city_origins","city_origin")
      .withColumnRenamed("administrative_regions","administrative_region")
      .withColumnRenamed("urban_agglomerationss","urban_agglomerations")
      .withColumnRenamed("city_grades","city_grade")
      .withColumnRenamed("economic_divisions","economic_division")
      .withColumnRenamed("registration_institutions","registration_institution")
      .withColumnRenamed("aedzids","aedzid")
      .withColumnRenamed("if_citys","if_city")
      .withColumnRenamed("if_districts","if_district")
      .withColumnRenamed("if_state_level_new_areass","if_state_level_new_areas")
      .withColumnRenamed("latitudes","latitude")
      .withColumnRenamed("longitudes","longitude")
      .withColumnRenamed("poor_countiess","poor_counties")
      .withColumnRenamed("rural_demonstration_countiess","rural_demonstration_counties")
      .withColumnRenamed("rural_ecommerces","rural_ecommerce")
      .withColumnRenamed("the_belt_and_road_citys","the_belt_and_road_city")
      .withColumnRenamed("the_belt_and_road_provinces","the_belt_and_road_province")
      .withColumnRenamed("the_yangtze_river_economic_zone_citys","the_yangtze_river_economic_zone_city")
      .withColumnRenamed("the_yangtze_river_economic_zone_provinces","the_yangtze_river_economic_zone_province")
      .withColumnRenamed("towns","town")


    val result = frame.toJSON.rdd.map(line => {
      val lines = JSON.parseObject(line)
      val evaluates = lines.getOrDefault("evaluates", "-1").toString
      val Base_Info = lines.getOrDefault("Base_Info", "-1").toString
      var ev = "-1"
      var str = new JSONObject
      if (evaluates.equals("-1")) {
        ev = "{\"fuyi\":\"-1\"}"
      } else {
        str = JSON.parseObject(evaluates)
      }
      if (!ev.contains("fuyi")) {
        lines.put("evaluates", str)
      } else {
        val evs = JSON.parseObject(ev)
        lines.put("evaluates", evs)
      }
      lines.put("Base_Info",Base_Info)
      lines
    })
    result.saveToEs(s"2019_tmall/tmall_2019_10", Map("es.mapping.id" -> "good_id"))
    sc.stop()

  }

}
